package com.ruoyi.web.core.config;


import com.amazonaws.AmazonServiceException;
import com.amazonaws.ClientConfiguration;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.AWSCredentialsProvider;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.client.builder.AwsClientBuilder;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;

import java.io.File;
import java.nio.file.Paths;

/**
 * @ClassName JDCloudOSS
 * @Description 文件上传到京东云OSS的初始化类
 * @Author 柚子茶
 * @Date 2021/1/24 15:05
 * @Version 1.0
 */
public class JDCloudOSS {

	/*
	private final String accessKey = "43BF9EBA89C900C8F67DD88FAF167930";

	private final String secretKey = "6E7244CF86408F8C5724CECDB65F98E1";

	private final String endpoint = "https://s3.cn-north-1.jcloudcs.com";

	private final String bucketName = "fkhd";

	private final String httpJDCloudUrl = "https://s3.cn-north-1.jdcloud-oss.com/fkhd/";
	*/
	
	private final String accessKey = "43BF9EBA89C900C8F67DD88FAF167930";

	private final String secretKey = "6E7244CF86408F8C5724CECDB65F98E1";

	private final String endpoint = "https://s3.cn-north-1.jcloudcs.com";

	private final String bucketName = "video20201216";

	private final String httpJDCloudUrl = "https://s3.cn-north-1.jdcloud-oss.com/video20201216/";


	/**
	 * @param filePath 文件上传路径
	 * @return String
	 * @description 上传文件到京东云OSS
	 * @author 柚子茶
	 * @date 2021/1/24 15:13
	 **/
	public String uploadFileJDCloud(String filePath) {
		AmazonS3 s3 = initS3();
		String fileName = Paths.get(filePath).getFileName().toString();
		File f =  new File(filePath);
		System.out.println("fileSize:"+f.length());
		try {
			s3.putObject(bucketName, fileName, new File(filePath));
			return httpJDCloudUrl + fileName;
		} catch (AmazonServiceException e) {
			e.printStackTrace();
			 
		}
		return httpJDCloudUrl + fileName;
	}


	/**
	 * @return AmazonS3
	 * @description 初始化京东云OSS连接客户端
	 * @author 柚子茶
	 * @date 2021/1/24 15:11
	 **/
	private AmazonS3 initS3() {

		ClientConfiguration config = new ClientConfiguration();

		AwsClientBuilder.EndpointConfiguration endpointConfig =
				new AwsClientBuilder.EndpointConfiguration(endpoint, "cn-north-1");

		AWSCredentials awsCredentials = new BasicAWSCredentials(accessKey, secretKey);
		AWSCredentialsProvider awsCredentialsProvider = new AWSStaticCredentialsProvider(awsCredentials);

		AmazonS3 s3 = AmazonS3Client.builder()
				.withEndpointConfiguration(endpointConfig)
				.withClientConfiguration(config)
				.withCredentials(awsCredentialsProvider)
				.disableChunkedEncoding()
				.withPathStyleAccessEnabled(true)
				.build();

		return s3;
	}


}